/*
 * 文 件 名 : PlanNewsAdapter.java
 * 版    权 : China Machi.Co.,Ltd.Copyright 2009-2011.All rights reserved
 * 描    述 : <描述>
 * 修 改 人 : lixg
 * 修改时间 : 2013-2-17
 * 跟踪单号 : <跟踪单号>
 * 修改单号 : <修改单号>
 * 修改内容 : <修改内容>
 */
package com.fire8.dolphin.ui.plan;

import java.util.List;

import com.fire8.dolphin.model.Plan;
import com.fire8.dolphin.ui.R;
import com.fire8.dolphin.ui.plan.PlanListAdapter.ViewHolder;
import com.fire8.dolphin.utils.LogUtil;
import com.fire8.dolphin.utils.StringUtil;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 计划动态列表适配器
 * <功能详细描述>
 *
 * @author  lixg
 * @version [版本号,2013-2-17]
 * @see     [相关类/方法]
 * @since   [产品/模块]
 */
public class PlanNewsAdapter extends BaseAdapter
{
    private List<Plan> planList;
    private LayoutInflater mInflater;
    private Context mContext;
    private final int TOTAL_TYPE_COUNT = 2;
    
    public PlanNewsAdapter(Context c,List<Plan> list)
    {
        mContext = c;
        mInflater = LayoutInflater.from(mContext);
        planList = list;
    }
    
    public void setPlanList(List<Plan> list)
    {
        planList = list;
    }
    
    @Override
    public int getItemViewType(int position)
    {
        return getItem(position).getDataType();
    }

    @Override
    public int getViewTypeCount()
    {
        return TOTAL_TYPE_COUNT;
    }

    @Override
    public boolean isEnabled(int position)
    {
        switch (getItem(position).getDataType()) {
            case 0:
                return false;
            case 1:
                return true;
            default:
                return false;
        }
    }

    /**
     * @return
     */
    @Override
    public int getCount()
    {
        if(planList != null)
        {
            return planList.size();
        }
        return 0;
    }
    
    /**
     * @param arg0
     * @return
     */
    @Override
    public Plan getItem(int arg0)
    {
        return planList.get(arg0);
    }
    
    /**
     * @param arg0
     * @return
     */
    @Override
    public long getItemId(int arg0)
    {
        return arg0;
    }
    
    /**
     * @param arg0
     * @param arg1
     * @param arg2
     * @return
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        Plan item = getItem(position);
        ViewHolder holder = null;
        
        switch (item.getDataType())
        {
            case 0://分类标题
                if (convertView == null) 
                {
                    holder = new ViewHolder();
                    convertView = new TextView(mContext);
                    holder.tvTitle = (TextView)convertView;
                }else
                {
                    holder = (ViewHolder) convertView.getTag();
                }
                holder.tvTitle.setBackgroundColor(mContext.getResources().getColor(R.color.list_bg));
                holder.tvTitle.setTextColor(mContext.getResources().getColor(R.color.font_gray));
                holder.tvTitle.setText(item.getTitle());
                holder.tvTitle.setPadding(2, 2, 2, 2);
                convertView.setTag(holder);
                break;
            case 1://正常计划
                if (convertView == null) 
                {
                    holder = new ViewHolder();
                    convertView = mInflater.inflate(R.layout.plan_news_item,null);
                    holder.tvUserName = (TextView)convertView.findViewById(R.id.tv_user_name);
                    holder.tvTitle = (TextView)convertView.findViewById(R.id.tv_title);
                    holder.tvTime = (TextView)convertView.findViewById(R.id.tv_alarm_time);
                    holder.imgOper = (ImageView)convertView.findViewById(R.id.img_opera);
                }else
                {
                    holder = (ViewHolder) convertView.getTag();
                }
                
                holder.tvTitle.setTag(item.getUserId());
                
                String userName = item.getUserName();
                if(StringUtil.isEmpty(userName))
                {
                    userName="佚名";
                }
                //LogUtil.d("userName="+userName);
                //LogUtil.d("AlarmTime="+item.getAlarmTime());
                holder.tvUserName.setText(userName);
                holder.tvTitle.setText(item.getTitle());
                try
                {
                    String alarmTime = item.getAlarmTime();
                    String[] split = alarmTime.split(" ");
                    holder.tvTime.setText(split[1]);
                }
                catch (Exception e)
                {
                    
                }
                convertView.setTag(holder);
                break;
            default:
                break;
        }
        return convertView;
    }
    public class ViewHolder
    {
        public TextView tvUserName;
        public TextView tvTitle;
        public TextView tvTime;
        public ImageView imgOper;//右边显示的箭头 >
        
    }
}
